// userPageC/pages/swimAround/swimAround.js
const app = getApp(); 
const WXAPI = require("../../../api/api");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1,
      // 是否显示左上角图标   1表示显示    0表示不显示
      title: '联联周边游',
      // 导航栏 中间的标题
      textColor: '#222',
      // 标题颜色
      bgColor: '#fff',
      // 导航栏背景颜色
      isloading: true,
      isColor: true 
    },
    navList:[
      {id: '', name: '全部'},
      {id: 1, name: '餐饮'},
      {id: 2, name: '休闲娱乐'},
      {id: 3, name: '酒店住宿'},
      {id: 4, name: '丽人'},
      {id: 5, name: '餐饮'},
      {id: 6, name: '休闲娱乐'},
      {id: 7, name: '酒店住宿'},
      {id: 8, name: '丽人'},
    ],
    addrShow: false,
    current: '',
    
    pageNum: 1,
    pageSize: 10,
    lng: '',
    lat: '',
    cityData: {},
    city: '',
    voucherList: [],
    locationId: '',
    isPull: true,//是否刷新
    lodingtype: 1,//加载状态（0：加载中  1：加载完成没有更多）
    pageSize: 10,
    pageNum: 1,
    jtkCode: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var jtkCode = wx.getStorageSync('jtkCode')
    this.setData({
      jtkCode: jtkCode,
      lng: app.globalData.lnt,
      lat: app.globalData.lat,
    })
    this.getCurrentSite()
    console.log('jtkCode',jtkCode)
  },
  getCurrentSite(){
    var data = {
      lng: this.data.lng,
      lat: this.data.lat,
    }
    WXAPI.currentSite( data, (res) =>{
      const data = res.result
      this.setData({
        cityData: data,
        city: data.city,
        locationId: data.id,
      })
      this.getList()
    })
  },
  getList(){
    const that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    var data = {
      jtkCode: this.data.jtkCode,
      locationId: this.data.locationId,
      lnt: this.data.lng,
      lat: this.data.lat,
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum,
    }
    WXAPI.productList(data, res => {
      const {size,total,records} = res.result;
      var list = [];
      var voucherList = this.data.voucherList;
      if (res.code === 200) {
        if (this.data.pageNum >= Math.ceil(total / size)) {
          this.setData({
            lodingtype: 1,
            isPull: false
          });
        } else {
          this.setData({
            lodingtype: 0,
            isPull: true
          });
        }

        wx.hideLoading();
        list = records, this.setData({
          voucherList: voucherList.concat(list)
        });
      }
    });
  },
  /* 选择站点 */
  handleAddrShow(){
    this.setData({
      addrShow: true,
    })
  },
  choseAddr(e){
    console.log('e,',e.detail)
    this.setData({
      addrShow: false,
    })
  },
  handleDetail(e){
    var url = e.currentTarget.dataset.url
    var arr  = url.split('?')
    var data = arr[1].split("&")
    console.log('url',data)
    var obj = {}
    data.forEach(item =>{
      var str = item.split('=')
      obj[str[0]] = str[1]
      
    })
    obj = JSON.stringify(obj)
    wx.navigateTo({
      url: '/userPageC/pages/externalH5/externalH5?url='+arr[0]+'&data='+obj+'&type=true'
    });
  },
  setCity(e){
    console.log('e,',e.detail)
    this.setData({
      locationId: e.detail.id,
      city: e.detail.city,
      pageNum: 1,
      voucherList: [],
    })
    this.getList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isPull == true) {
      var pageNum = this.data.pageNum;
      pageNum++;
      console.log("触底了", pageNum);
      this.setData({
        pageNum: pageNum
      });
      this.getList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})